<?php

namespace app\api\model;

class UserWithdrawModel extends BaseModel
{
    protected $pk = 'id';
    protected $table = 'hs_user_withdraw';
    protected static $tableName = 'user_withdraw';

    //审核状态
    const STATUS_DEFAULT = 0;//待处理
    const STATUS_SUCCESS = 1;//审核通过
    const STATUS_FAIL = 2;//审核失败

    /**
     * @return \think\model\relation\HasOne
     */
    public function user()
    {
        return $this->hasOne('app\api\model\UserModel', 'id', 'user_id');
    }

    //获取提现中金额
    public static function withdrawingMoney($user_id, $type)
    {
        return self::where('user_id', $user_id)->where('type', $type)->where('status', self::STATUS_DEFAULT)->sum('amount');
    }

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public static function status()
    {
        return [
            self::STATUS_DEFAULT => '审核中',
            self::STATUS_SUCCESS => '已到账',
            self::STATUS_FAIL => '拒绝提现'
        ];
    }

    /**
     * 生成唯一提现订单号
     * @return string
     */
    public static function getNewWithdrawId()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $no = $msectime . mt_rand(10000, 99999);
        if (self::where('withdraw_id', $no)->find()) {
            $no = $msectime . mt_rand(10000, 99999);
        }
        return $no;
    }


    public static function lst($params)
    {
        $page = isset($params['page']) ? $params['page'] : 1;
        $limit = isset($params['limit']) ? $params['limit'] : 5;
        $list = self::where('user_id', $params['user_id']);
        if (isset($params['type']) && $params['type']) {
            $list = $list->where('type', $params['type']);
        }
        $list = $list->field('id,amount,status,createtime')
            ->page((int)$page, (int)$limit)->order('id desc')
            ->select();
        return $list;
    }


}